# This script creates the first three plots presented in the main paper in order listed in the paper.
# Since Fig. 4 requires the models presented in the paper, the code for Fig. 4 can be found in the "Analysis.R" script.
# Required packages: tidyverse, ggridges, forcats
# Required data files: data_basic.RData
# Contact for any questions or clarifications: Dino Wildi, to be reached at dino.wildi@ucdconnect.ie

library(tidyverse)
library(ggridges)

load("data_basic.RData")

#### FIGURE 1: COMBINED BOX/VIOLIN PLOT POSITION ####

fig1 <- ggplot(data, aes(x = as.factor(msp), y = multicult)) +
  geom_violin(aes(fill = as.factor(msp))) + geom_boxplot(alpha = 0.5) +
  labs(x = "Party Type", y = "Anti-multiculturalism") +
  scale_x_discrete(labels = c("Radical right", "Mainstream")) +
  theme_minimal() + 
  theme(legend.position = "none",
        axis.title = element_text(size = 20),
        axis.text = element_text(size = 18))

fig1

#Alternative: split by Big Three

partyfam <- mutate(data, type = case_when(
  parfam == 30 ~ "Social Democrat",
  parfam %in% c(40, 80) ~ "Liberal",
  parfam %in% c(50, 60, 90) ~ "Christian Democrat\nConservative",
  parfam == 70 ~ "Radical Right")) %>% 
  group_by(type)
partyfam <- mutate(partyfam, freq = n(), max = max(multicult))

fig1.2 <- ggplot(partyfam, 
                 aes(x = fct_reorder(type, multicult), 
                     y = multicult)) +
  geom_violin(aes(fill = type)) + geom_boxplot(alpha = 0.5) +
  geom_text(aes(label = paste("n = ",freq),
                x = type,
                y = max + 0.25)) +
  labs(x = "Party Family", y = "Anti-multiculturalism") +
  theme_minimal() + 
  theme(legend.position = "none",
        axis.title = element_text(size = 20),
        axis.text = element_text(size = 18))

fig1.2

#### FIGURE 2: ELECTORAL OVERLAP VISUALISATION OVERALL ####

plotset.fig2 <- filter(data, msp == 1)

fig2 <- ggplot(plotset.fig2, aes(x = wagner_winback, y = wagner_loss)) +
  geom_point() + geom_smooth(method = 'lm') +
  labs(x = "Potential to gain votes from AIP", y = "Threat of losing votes to AIP") +
  theme_minimal() +
  theme(axis.title = element_text(size = 20),
        axis.text = element_text(size = 18))

fig2

#### FIGURE 3: ELECTORAL OVERLAP VISUALISATION, BY COUNTRY ####

#preparation for faceting
plotset.fig3 <- filter(data, msp == 1 & !(is.na(wagner_winback))) %>% 
  pivot_longer(cols = c(wagner_loss, wagner_winback),
               names_to = "direction",
               values_to = "availability")

#labels for facets
facets <- c("MSP voter availability to RRP", "RRP voter availability to MSP")
names(facets) <- c("wagner_loss", "wagner_winback")

#Plot
fig3 <- ggplot(plotset.fig3) +
  geom_density_ridges(aes(x = availability, 
                          y = fct_reorder(countryname, desc(availability))), 
                      alpha = 0.5) +
  facet_wrap(~direction,
             labeller = labeller(direction = facets)) +
  labs(x = "Availability", y = NULL) +
  theme_minimal() +
  theme(axis.text = element_text(size = 12),
        strip.text = element_text(size = 14),
        axis.title.x = element_text(size = 14))
fig3

